﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIDialog : UIPanal 
{
	private RectTransform rectTransform = null;
	private float? uiX = null;
	private float? uiY = null;

	// Use this for initialization
	void Start () 
	{
		if (uiX != null && uiY != null) 
		{
			getRectTransform().anchoredPosition = new Vector2 ((float)uiX, (float)uiY);
		}
			
	}
	
	// Update is called once per frame
	void Update () 
	{
		
	}
		
	public UICompontent addChild<T>(T dialogClass) where T:UICompontent
	{
		return addChildTo (dialogClass, transform);
	}

	public UICompontent addChildTo<T>(T dialogClass, Transform target) where T:UICompontent
	{
		Object obj = Resources.Load(dialogClass.path(), typeof(GameObject));
		GameObject gameObj = GameObject.Instantiate(obj) as GameObject;
		if (gameObj == null)
			return null;

		var dClass = gameObj.AddComponent<T> ();
		dClass.transform.parent = target;
		dClass.name = typeof(T).ToString();

		dClass.getRectTransform().localScale = new Vector3 (1, 1, 1);

		return dClass;
	} 

	public void setPostion(float x, float y)
	{
		if (getRectTransform() == null) 
		{
			uiX = x;
			uiY = y;
		} 
		else 
		{
			rectTransform.anchoredPosition = new Vector2 (x, y);
		}
	}

	public RectTransform getRectTransform()
	{ 
		if (rectTransform == null) 
		{
			var g = gameObject.transform.GetComponent<RectTransform> ();
			rectTransform = (RectTransform)gameObject.transform.GetComponent<RectTransform> ();
		}
		return rectTransform;		
	}

	public void close()
	{
		Destroy (this);
	}

	public virtual string path()
	{
		return "UIDialog/UIScene";
	}

	public virtual string panal()
	{
		return UIManager.panelLayerName.scene.ToString();
	}
}
